import httpclient from 'httpclient';
import api from '../../../../api/api_kind.js';
export default {
    name: "role",
    data: function () {
        return {
            editTitle: "新增角色",
            table: {
                api: api.BASE.ROLE.ROLE_LIST_PAGE,//角色列表api
                isOpen:true,//是否使用展开行
                header:[
                    {"prop": "code", "label": "路由名称",isOpen:false},
                    {"prop": "name", "label": "角色名称",isOpen:false},
                    {"prop": "description", "label": "描述",isOpen:true},
                    {"prop": "creator", "label": "创建者",isOpen:true},
                    {"prop": "updater", "label": "更新者",isOpen:true},
                ],
                searchForm:{//查询条件
                    name: ""
                }
            },
            detailSlider: {//修改对象
                data: []
            },
            //产品信息编辑对话框
            dataDialog: {
                visible: false, //显示开关
                isVisible: true,//是否显示重置按钮,新增有重置按钮
                data: {},//传参数据
                ids:[],//删除
                rules: {
                    code: [{required: true, message: '请输入路由名称', trigger: 'blur'}],
                    name: [{required: true, message: '请输入角色名称', trigger: 'blur'}]
                },
                disabled: false //是否禁用
            },
            url:"https://blog.csdn.net/wojiaomaxiaoqi/article/details/79416039"
        };
    },
    methods:{
        resetProductMes() {
            this.dataDialog.data = {};
            this.$refs.dataDialogForm.resetFields();
        },//重置表单
        onSearch() {//刷新数据
            this.$refs.element_table.loadData();
        },
        handleSelectionChange(val){
            this.dataDialog.ids = val;
        },
        onDelete(row) {//调用删除产品接口
            const that = this;
            let ids = '';
            if (undefined !== row.id && that.dataDialog.ids.length===0) {//单条删除
                ids = row.id;
            }else if(undefined === row.id && that.dataDialog.ids.length!==0){//批量删除
                ids = that.dataDialog.ids.map(item => item.id).join();
            }else{
                httpclient.errorPage("error", "请选择一条数据", true);
                return;
            }
            that.$confirm('此操作将删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                httpclient.sendDelete(api.BASE.ROLE.DEL_ROLE, {ids:ids}).done((res) => {
                    if (res.code === 1) {
                        that.onSearch();
                        httpclient.errorPage("success", "删除成功", true);
                    } else {
                        httpclient.errorPage("error", "获取数据失败", true);
                    }
                }).fail((error) => {
                    httpclient.errorPage("error", error.errorMsg, true);
                });
            }).catch(() => {
                httpclient.errorPage("warning", "删除异常", true);
            });
        },
        saveData() {//保存产品
            const that = this;
            this.$refs['dataDialogForm'].validate((valid) => {//element表单验证
                if (valid) {//如果验证通过
                    if (that.dataDialog.isVisible) {
                        httpclient.sendPut(api.BASE.ROLE.ADD_ROLE, that.dataDialog.data).done((res) => {
                            if (res.code === 1) {
                                that.onSearch();
                                httpclient.errorPage("success", "保存成功", true);
                                that.dataDialog.visible = false;
                                that.resetProductMes();
                            } else {
                                httpclient.errorPage("error", "获取数据失败", true);
                            }
                        }).fail((error) => {
                            httpclient.errorPage("error", error.errorMsg, true);
                        });
                    } else {
                        that.detailSlider.data.id = that.dataDialog.data.id;
                        that.detailSlider.data.name = that.dataDialog.data.name;
                        that.detailSlider.data.code = that.dataDialog.data.code;
                        httpclient.sendPut(api.BASE.ROLE.ADD_ROLE, that.detailSlider.data).done((res) => {
                            if (res.code === 1) {
                                that.onSearch();
                                httpclient.errorPage("success", "修改成功", true);
                                that.dataDialog.visible = false;
                            } else {
                                httpclient.errorPage("error", "获取数据失败", true);
                            }
                        }).fail((error) => {
                            httpclient.errorPage("error", error.errorMsg, true);
                        });
                    }
                } else {
                    return false;
                }
            });
        },
        onAdd() {//点击新增产品
            const that = this;
            that.editTitle = "新增角色";
            that.dataDialog.data = {};
            that.dataDialog.isVisible = true;
            that.dataDialog.disabled = false;
        },
        onEdit(row) {//点击编辑按钮
            const that = this;
            that.editTitle = "修改角色";
            httpclient.sendPut(api.BASE.ROLE.FIND_ONE_ROLE, {id:row.id}).done((res) => {
                if (res.code === 1) {
                    that.dataDialog.data = res.data;
                    that.dataDialog.isVisible = false;
                    that.dataDialog.disabled = false;
                } else {
                    httpclient.errorPage("error", "获取数据失败", true);
                }
            }).fail((error) => {
                httpclient.errorPage("error", error.errorMsg, true);
            });
        },
        onShow(row) {//点击查看按钮
            const that = this;
            that.editTitle = "查看角色";
            httpclient.sendPost(api.BASE.ROLE.FIND_ONE_ROLE, {id:row.id}).done((res) => {
                if (res.code === 1) {
                    that.dataDialog.data = row;
                    that.dataDialog.disabled = true;
                } else {
                    httpclient.errorPage("error", "获取数据失败", true);
                }
            }).fail((error) => {
                httpclient.errorPage("error", error.errorMsg, true);
            });
        },
    }
};
